<template>
  <!-- 登陆页面样式 -->
  <div class="login">
    <el-form ref="form" :model="form" :rules="rules">
      <!-- 登录页logo -->
      <el-form-item>
        <img class="logoImg" src="@/assets/common/login-logo.png" alt="">
      </el-form-item>
      <!-- 用户名输入框 -->
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          placeholder="请输入用户名"
          prefix-icon="el-icon-user-solid"
        />
      </el-form-item>
      <!-- 密码输入框 -->
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          placeholder="请输入密码"
          prefix-icon="el-icon-lock"
          show-password
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <!-- 登录按钮 -->
      <el-form-item>
        <el-button class="btn" type="primary" @click="submit">登录</el-button>
      </el-form-item>
      <!-- 提示信息区域 -->
      <div class="tips">用户名：13800000002, 密码：123456</div>
    </el-form>
  </div>
</template>

<script>
// 导入 用户登录接口
// import { sysLogin } from '@/api/user.js'

export default {
  data() {
    return {
      form: {
        mobile: '', // 用户名
        password: '' // 密码
      },
      // 表单验证
      rules: {
        mobile: [
          { required: true, message: '请输入用户名', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              const res =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              res.test(value)
                ? callback()
                : callback(new Error('请输入正确的用户名'))
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 设置方法-----点击登录按钮触发
    submit() {
      // 点击登录应该触发 全局表单验证
      this.$refs.form.validate(async(result) => {
        if (result) {
          // 调用用户登录接口
          // const res = await sysLogin(this.form)
          // console.log(res)
          await this.$store.dispatch('user/userLogin', this.form)
          this.$message.success('登录成功')
          // console.log('验证成功')
          // 登录成功，跳转首页
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg') no-repeat;
  background-position: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  form.el-form {
    width: 436px;
    .logoImg {
      margin-bottom: 20px;
    }
    .btn {
      background: #407ffe;
      width: 100%;
      height: 60px;
      font-size: 24px;
    }
  }
  ::v-deep input.el-input__inner {
    height: 52px;
  }
  ::v-deep .el-form-item__error {
    color: #fff;
  }
}
</style>
